<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Art, deleteArt, checkStatusArt } from "@/http/api";

export default {
  components: { outMain },
  name: "",
  data() {
    return {
      tableData: [],

      queryData: {
        time: "",
        article_title: "",
        page: 1,
        per_page: 10,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    deleteFn(row) {
      ElMessageBox.confirm("确定删除选择的公告吗?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const { data } = await deleteArt({ article_id: row.article_id });
        if (data.code == 1) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          this.getData();
        }
      });
    },
    addFn(id) {
      this.$router.push({ path: "/setNoticeAdd", query: { id: id } });
    },
    questCheck(data) {
      checkStatusArt(data).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "操作成功",
          });
          this.getData();
        }
      });
    },
    checkStatus(row, type) {
      let str = type == 1 ? "启用" : "关闭";
      ElMessageBox.confirm(`确定${str}选择的公告吗?`, "操作提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let data = {
          article_id: row.article_id,
          article_status: type,
        };
        this.questCheck(data);
      });
    },
    getTime(time) {
      const y = time.getFullYear();
      const m =
        time.getMonth() < 9 ? `0${time.getMonth() + 1}` : time.getMonth() + 1;
      const d = time.getDate() < 10 ? `0${time.getDate()}` : time.getDate();
      const str = `${y}-${m}-${d}`;
      return str;
    },
    getData() {
      let paramsData = { ...this.queryData };
      if (paramsData.time) {
        let start = this.getTime(paramsData.time[0]);
        let end = this.getTime(paramsData.time[1]);
        paramsData.time = start + "," + end;
      }
      Art(paramsData).then((res) => {
        if (res.data.code == 1) {
          this.tableData = res.data.data.data;
          this.total = res.data.data.total;
        }
      });
    },
  },
  mounted() {},
  created() {
    this.getData();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>公告列表（{{ total }}）</div>
      </div>
      <div class="tableSerch">
        <div class="serchLeft">
          <btn value="新增" @ok="addFn"></btn>
        </div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.article_title"
                placeholder="请输入公告名称"
              />
            </el-form-item>
            <el-form-item style="width: 300px" class="time">
              <el-date-picker
                @change="getData"
                clearable
                style="width: 280px"
                v-model="queryData.time"
                type="daterange"
                range-separator="~"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
              />
            </el-form-item>
            <btn value="搜索" @click="getData"></btn>
          </el-form>
        </div>
      </div>
      <el-table class="tableMain" :data="tableData" stripe style="width: 100%">
        <el-table-column
          prop="article_title"
          show-overflow-tooltip
          width="250"
          label="公告标题"
        />
        <el-table-column
          prop="article_content"
          show-overflow-tooltip
          label="公告内容"
        />
        <el-table-column width="100" show-overflow-tooltip label="启用状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag type="success" v-if="row.article_status == 1"
                >启用中</el-tag
              >
              <el-tag type="info" v-if="row.article_status == 2">已关闭</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="article_type"
          width="120"
          show-overflow-tooltip
          label="公告平台"
        />

        <el-table-column
          width="180"
          prop="create_time"
          show-overflow-tooltip
          label="创建时间"
        />
        <el-table-column prop="Name" width="140" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div class="redText ctrlText" @click="deleteFn(row)">删除</div>
              <div
                class="greenText ctrlText"
                @click="checkStatus(row, 1)"
                v-if="row.article_status == 2"
              >
                启用
              </div>
              <div
                class="redText ctrlText"
                @click="checkStatus(row, 2)"
                v-if="row.article_status == 1"
              >
                关闭
              </div>
              <div class="blueText ctrlText" @click="addFn(row.article_id)">
                编辑
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
